package stock.subjects.Imp;

import stock.chart.BasicChart;
import stock.chart.Chart;
import stock.entity.PriceQuote;
import stock.entity.TradingStatus;
import stock.observers.Observer;
import stock.subjects.Subject;

import java.math.BigDecimal;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;

public class StockData implements Subject {
    private List<Observer> observers;
    /** 股票代码（如 AAPL） */
    private String stockticker;
    /** 股票名称（如 Apple Inc.） */
    private String stockname;
    /** 最小交易单位/每手股数 */
    private int tradesize;
    /** 交易状态 */
    private TradingStatus tradingstatus;
    /** 最新行情快照 */
    private PriceQuote latestQuote;
    /** 最近一次实体更新的时间 */
    private Instant time;
    /** 总市值（与业务币种口径一致） */
    private BigDecimal marketCap;
    /** 数据来源标识（如 Refinitiv、ExchangeFeed） */
    private String dataSource;

    private Chart chart;

    public Chart getChart() {
        return chart;
    }

    public StockData() {
        observers = new ArrayList<Observer>();
        chart = new BasicChart();
    }

    @Override
    public void RegisterObserver(Observer o) {
        observers.add(o);
    }

    @Override
    public void RemoveObserver(Observer o) {
        for(int i = 0; i < observers.size(); i++){
            if((Observer) observers.get(i) == o){
                observers.remove(i);
            }
        }
    }

    @Override
    public void NotifyObservers() {
        for (Observer o : observers){
            o.update(stockticker, stockname, tradesize, tradingstatus, latestQuote, time, marketCap, dataSource, chart);
        }
    }

    public void dataChanged(){
        NotifyObservers();
    }

    public void setData(String stockticker, String stockname, int tradesize, TradingStatus tradingstatus, PriceQuote latestQuote, Instant time, BigDecimal marketCap, String dataSource, Chart chart) {
        this.stockticker = stockticker;
        this.stockname = stockname;
        this.tradesize = tradesize;
        this.tradingstatus = tradingstatus;
        this.latestQuote = latestQuote;
        this.time = time;
        this.marketCap = marketCap;
        this.dataSource = dataSource;
        if (chart != null) {
            this.chart = chart;
        }
        dataChanged();
    }

    public String getStockticker() {
        return stockticker;
    }

    public void setStockticker(String stockticker) {
        this.stockticker = stockticker;
    }

    public String getStockname() {
        return stockname;
    }

    public void setStockname(String stockname) {
        this.stockname = stockname;
    }

    public int getTradesize() {
        return tradesize;
    }

    public void setTradesize(int tradesize) {
        this.tradesize = tradesize;
    }

    public TradingStatus getTradingstatus() {
        return tradingstatus;
    }

    public void setTradingstatus(TradingStatus tradingstatus) {
        this.tradingstatus = tradingstatus;
    }

    public PriceQuote getLatestQuote() {
        return latestQuote;
    }

    public void setLatestQuote(PriceQuote latestQuote) {
        this.latestQuote = latestQuote;
    }

    public Instant getTime() {
        return time;
    }

    public void setTime(Instant time) {
        this.time = time;
    }

    public BigDecimal getMarketCap() {
        return marketCap;
    }

    public void setMarketCap(BigDecimal marketCap) {
        this.marketCap = marketCap;
    }

    public String getDataSource() {
        return dataSource;
    }

    public void setDataSource(String dataSource) {
        this.dataSource = dataSource;
    }
}